package net.oschina.app;

import android.os.Environment;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.robotium.solo.Solo;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by Aaron on 2017/6/11.
 */

public class BasicTestCase extends ActivityInstrumentationTestCase2 {
    private Solo solo1;
    private SoloWrap solo;
    protected Helper helper;
    private boolean isNeedLogin = false;

    public BasicTestCase(){
        super(AppStart.class);
    }

    public void setUp(boolean isNeedLogin)throws Exception{
        FileWriter fw = new FileWriter(Environment.getExternalStorageDirectory()+"/result.txt");
        fw.write("helloworld");;
        fw.flush();
        fw.close();
        this.isNeedLogin = isNeedLogin;
        this.setUp();
    }

    @Override
    public void setUp() throws Exception {
        try{
            super.setUp();
            logCurrentCaseName();
            solo1 = new Solo(getInstrumentation(),getActivity());
            solo = new SoloWrap(solo1);
            helper = new Helper(solo);
            //需要登录的情况：
            if(isNeedLogin){
                helper.getPageNavigation().clickMyTab();
                if(!helper.getPageCommon().waitForText("autotest",2)){
                    helper.getPageMyTab().clickLoginIcon();
                    helper.getPageLogin().login("735723619@qq.com","12345678");
                }
            }else{
                //不需要登录的情况
                helper.getPageNavigation().clickMyTab();
                solo.sleep(2);
                if(helper.getPageCommon().searchText("autotest")){
                    helper.getPageMyTab().clickSettingsIcon();
                    helper.getPageSettings().clickLogout();
                }
            }
            helper.getPageNavigation().clickNewsTab();
        }catch (Exception th){
            Log.i("AUTO","the exception happened in setUp section +" + th.getCause());
            solo.takeScreenShot(this.getClass().getSimpleName());
            throw th;
        }

    }

    @Override
    protected void runTest() throws Throwable {
        try{
            super.runTest();
        }catch (Throwable th){
            Log.i("AUTO","Exception happened in runtest sceiont, the eccption msg is "+th.getMessage());
            solo.takeScreenShot(this.getClass().getSimpleName());
            throw th;
        }

    }

    @Override
    public void tearDown() throws Exception {
        try{

        }catch(Exception ex){
            Log.i("AUTO","Exception happened in teardown sceiont, the eccption msg is "+ex.getMessage());
            solo.takeScreenShot(this.getClass().getSimpleName());
            throw ex;
        }finally {
            solo1.finishOpenedActivities();
            super.tearDown();
        }

    }

    public void logCurrentCaseName(){
        FileWriter fw = null;
        try{
            fw = new FileWriter(Environment.getExternalStorageDirectory()+"/crash.txt");
            fw.write(this.getClass().getName());
            fw.flush();
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
